package com.chinacoal.hr.levelimpl.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chinacoal.hr.levelapi.client.LevelRecordClient;
import com.chinacoal.hr.levelapi.client.response.Result;
import com.chinacoal.hr.levelapi.client.response.ResultGenerator;
import com.chinacoal.hr.levelapi.entity.LeaveRecord;
import com.chinacoal.hr.levelimpl.service.LeaveRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: YunTao.Li
 * @create: 2019-11-12 18:34
 * @description:
 **/
@RestController
public class LevelRecordController implements LevelRecordClient{

    @Autowired
    LeaveRecordService recordService;

    /**
     * 查询请假记录列表
     *
     * @param page           : 当前页数
     * @param limit          : 每页条数
     * @param orgCode        : 组织机构
     * @param holidayType    : 假期类型
     * @param businessStatus : 审批状态
     * @param startDate      : 开始时间
     * @param endDate        : 结束时间
     * @param queryRange     :查询范围（all、dept、person）
     * @return : com.chinacoal.hr.levelapi.response.Result
     * @author : YunTao.Li
     * @date : 2019/11/15 2019/11/15
     */
    public Result list(long page,
                       long limit,
                       String orgCode,
                       String deptCode,
                       String deptName,
                       String empName,
                       String empCode,
                       String holidayType,
                       String businessStatus,
                       String startDate,
                       String endDate,
                       String queryRange) {

        // 构建查询条件
        Map<String, Object> condition = new HashMap<String, Object>();
        condition.put("page", page);
        condition.put("limit", limit);
        condition.put("orgCode", orgCode);
        condition.put("deptCode", deptCode);
        condition.put("deptName", deptName);
        condition.put("empName", empName);
        condition.put("empCode", empCode);
        condition.put("holidayType", holidayType);
        condition.put("businessStatus", businessStatus);
        condition.put("startDate", startDate);
        condition.put("endDate", endDate);
        condition.put("queryRange", queryRange);

        IPage<LeaveRecord> recordList = recordService.getLeaveRecordList(condition);
        return ResultGenerator.genOkResult(recordList);
    }

    /**
     * 新增请假单信息
     *
     * @param record :
     * @return : com.chinacoal.hr.levelapi.response.Result
     * @author : YunTao.Li
     * @date : 2019/11/15 2019/11/15
     */
    public Result add( LeaveRecord record) {
        Map<String, String> result = recordService.addLeaveRecord(record);

        if ("-1".equals(result.get("code"))) {
            String message = result.get("message");
            return ResultGenerator.genFailedResult(message);
        } else {
            return ResultGenerator.genOkResult();
        }
    }

    /**
     * 修改请假单信息
     *
     * @param record :
     * @return : com.chinacoal.hr.levelapi.response.Result
     * @author : YunTao.Li
     * @date : 2019/11/15 2019/11/15
     */
    public Result update( String levelId,
                          LeaveRecord record) {
        Map<String, String> result = recordService.updateLeaveRecord(record);

        if ("-1".equals(result.get("code"))) {
            String message = result.get("message");
            return ResultGenerator.genFailedResult(message);
        } else {
            return ResultGenerator.genOkResult();
        }
    }

    /**
     * 删除请假单
     *
     * @param levelId :
     * @return : com.chinacoal.hr.levelapi.response.Result
     * @author : YunTao.Li
     * @date : 2019/11/16 2019/11/16
     */
    public Result delete( String levelId) {
        Map<String, String> result = recordService.deleteLeaveRecord(levelId);

        if ("-1".equals(result.get("code"))) {
            String message = result.get("message");
            return ResultGenerator.genFailedResult(message);
        } else {
            return ResultGenerator.genOkResult();
        }
    }

    /**
     * 提交请假单信息
     *
     * @param record :
     * @return : com.chinacoal.hr.levelapi.response.Result
     * @author : YunTao.Li
     * @date : 2019/11/15 2019/11/15
     */
    public Result submit( String levelId,
                          LeaveRecord record) {
        try {
            Map<String, String> result = recordService.submitLeaveRecord(record);
            if ("-1".equals(result.get("code"))) {
                String message = result.get("message");
                return ResultGenerator.genFailedResult(message);
            } else {
                return ResultGenerator.genOkResult();
            }
        } catch (Throwable e) {
            return ResultGenerator.genFailedResult(e.getMessage());
        }
    }

    /**
     * 撤回请请假单
     *
     * @param levelId :
     * @param record  :
     * @return : com.chinacoal.hr.levelimpl.response.Result
     * @author : YunTao.Li
     * @date : 2019/12/12 2019/12/12
     */
    public Result drawback( String levelId,
                            LeaveRecord record) {
        try {
            Map<String, String> result = recordService.drawbackLeaveRecord(record);
            if ("-1".equals(result.get("code"))) {
                String message = result.get("message");
                return ResultGenerator.genFailedResult(message);
            } else {
                return ResultGenerator.genOkResult();
            }
        } catch (Throwable e) {
            return ResultGenerator.genFailedResult(e.getMessage());
        }
    }

}
